package com.wardrobe.soa.api.booking_order;

import com.wardrobe.domain.booking_order.BookingOrderbean;
import com.wardrobe.domain.common.BookingOrderPayFromBean;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/20.
 */
@RestController
@RequestMapping("/api/bookingorder")
public class BookingOrderController {
    private static final Logger logger = LoggerFactory.getLogger(BookingOrderController.class);
    @Autowired
    private BookingOrderService bookingOrderService;

    @RequestMapping("/saveBookingOrder")
    public ApiResult saveBookingOrder(BookingOrderbean fromBean){
        try {
            long orderId = bookingOrderService.saveBookingOrder(fromBean);
            if(orderId != 0){
                return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,orderId);
            }else {
                return new ApiResult(CommonConsant.bussinessFailedCode,CommonConsant.failed,"保存订单失败",0);
            }
        }catch (Exception e){
            logger.error("saveBookingOrder_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
    @RequestMapping("/modifyBookingOrderStatus")
    public ApiResult modifyOrderStatus(long orderId,int status){
        try {
            int flag = bookingOrderService.modifyOrderStatus(orderId,status);
            if(flag == 1){
                return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,flag);
            }else {
                return new ApiResult(CommonConsant.bussinessFailedCode,CommonConsant.failed,"修改订单失败",flag);
            }
        }catch (Exception e){
            logger.error("modifyBookingOrderStatus_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/bookingOrderList")
    public ApiResult bookingOrderList(long userId,int status){
        try {
            List<BookingOrderResponse> infos = bookingOrderService.bookingOrderList(userId, status);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,infos);
        }catch (Exception e){
            logger.error("bookingOrderList_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/getBookingOrderDetails")
    public ApiResult getBookingOrderDetails(long orderId){
        try {
            BookingOrderDetail bookingOrderDetails = bookingOrderService.getBookingOrderDetails(orderId);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,bookingOrderDetails);
        }catch (Exception e){
            logger.error("getOrderDetails_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
    @RequestMapping("weixinPay")
    public ApiResult weixinPay(BookingOrderPayFromBean fromBean){
        try {
            Map<String, Object> res = bookingOrderService.weixinPay(fromBean);
            logger.warn("weixinPay_info:{}",res);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,res);
        }catch (Exception e){
            logger.error("getOrderDetails_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("weixinCallBack")
    public ApiResult weixinCallBack(HttpServletRequest request, HttpServletResponse response){
        try {
            bookingOrderService.weixinCallBack(request,response);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,"");

        }catch (Exception e){
            logger.error("getOrderDetails_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
}
